package com.wangbo.box.command;

/**
 * 命令模式
 * <p>
 * 将一个请求封装为一个对象，使发出请求的责任和执行请求的责任分割开。这样两者之间通过命令对象进行沟通，这样方便将命令对象进行储存、传递、调用、增加与管理。
 */
public class CommandPattern {

    interface Command {
        void execute();
    }

    static class CommandA implements Command {
        Service service;

        public CommandA(Service service) {
            this.service = service;
        }

        @Override
        public void execute() {

            service.action();
        }
    }

    static class CommandB implements Command {
        Service service;

        public CommandB(Service service) {
            this.service = service;
        }

        @Override
        public void execute() {
            service.action();
        }
    }

    interface Service {
        void action();
    }

    static class ServiceA implements Service{

        @Override
        public void action() {
            System.out.println("service A action");
        }
    }

    static class ServiceB implements Service{

        @Override
        public void action() {
            System.out.println("service B action");
        }
    }

    static class Invoker{
        Command command;

        public Invoker(Command command) {
            this.command = command;
        }

        void call(){
            command.execute();
        }
    }
}
